package com.leon.alishop.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.leon.alishop.core.bo.ProviderBo;
import com.leon.alishop.core.bof.CityAreaBof;
import com.leon.alishop.core.bof.ProviderBof;
import com.leon.alishop.core.page.Page;
import com.leon.alishop.core.service.ProviderInfoService;
import com.leon.alishop.core.util.AliShopStringUtils;
import com.leon.alishop.core.vo.ProviderParams;
import com.leon.alishop.dao.ProviderInfoDao;
import com.leon.alishop.model.ProviderInfo;

@Controller
@RequestMapping(value = "/provider")
public class ProviderController extends BaseController{

	private static String EDIT_PAGE = "/provider/edit";
	
	private static String LIST_PAGE = "/provider/list";
	
	@RequestMapping(value = "/list")
	public String viewProviderList(ProviderParams params,
			BindingResult bindingResult, HttpServletRequest req,HttpServletResponse res,Model model){
		if(null == params)
			params = new ProviderParams();
		Page page = new Page(300);
		page.setPageNo(params.getP());
		List<ProviderBo> pbList = providerBof.showProviderBoList(params, page);
		model.addAttribute("params", params);
		model.addAttribute("page", page);
		model.addAttribute("providerBoList", pbList);
		return LIST_PAGE;
	}
	
	@RequestMapping(value = "/provider-edit")
	public String edit(ProviderParams params,
			BindingResult bindingResult, HttpServletRequest req,HttpServletResponse res,Model model){
		if(null == params)
			params = new ProviderParams();
		initModel(model);
		model.addAttribute("params", params);
		if(!"edit".equalsIgnoreCase(params.getReqType())){
			params.setReqType("edit");
			if(params.getId()!=null){
				ProviderInfo pro = providerInfoDao.selectById(params.getId());
				if(pro != null){
					try {
						BeanUtils.copyProperties(params, pro);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}else{
					params.setErrorMsg("该条记录不存在！");
				}
			}
			return EDIT_PAGE;
		}
		trimParams(params);
		params.setErrorMsg(this.checkValidParams(params));
		if(StringUtils.isNotBlank(params.getErrorMsg())){
			return EDIT_PAGE;
		}
		int ret = providerInfoService.insertOrUpdateProviderInfo(params);
		if(ret > 0){
			model.addAttribute("params", new ProviderParams());
		}
		return EDIT_PAGE;
	}
	
	private String checkValidParams(ProviderParams params){
		if(StringUtils.isBlank(params.getName())
				|| AliShopStringUtils.getCharLength(params.getName())>50){
			return "店铺名不能为空，且不能超过50个字符！";
		}
		if(StringUtils.isBlank(params.getTaobaoHomeUrl())
				|| AliShopStringUtils.getCharLength(params.getName())>300){
			return "URL不能为空，且不能超过300个字符！";
		}
		
		if(params.getAreaId() == null
				|| null == cityAreaBof.findCityAreaById(params.getAreaId()))
			return "请选择地区！";
		if(StringUtils.isBlank(params.getWangwang())
				|| AliShopStringUtils.getCharLength(params.getWangwang()) > 45)
			return "请输入正确的旺旺！";
		return null;
	}
	
	private void trimParams(ProviderParams params){
		params.setName(StringUtils.trim(params.getName()));
		params.setAddress(StringUtils.trim(params.getAddress()));
		params.setContactName(StringUtils.trim(params.getContactName()));
		params.setMobile(StringUtils.trim(params.getMobile()));
		params.setPhone(StringUtils.trim(params.getPhone()));
		params.setQq(StringUtils.trim(params.getQq()));
		params.setTaobaoHomeUrl(StringUtils.trim(params.getTaobaoHomeUrl()));
		params.setWangwang(StringUtils.trim(params.getWangwang()));
	}
	
	private void initModel(Model model) {
		model.addAttribute("cityAreaList", cityAreaBof.findAllCityArea());
	}
	
	@Autowired
	private CityAreaBof cityAreaBof;
	
	@Autowired
	private ProviderInfoService providerInfoService;
	
	@Autowired
	private ProviderInfoDao providerInfoDao;
	
	@Autowired
	private ProviderBof providerBof;
	
}
